package com.cl.master.controller;

import com.cl.master.common.Result;
import com.cl.master.domain.Admin;
import com.cl.master.domain.Note;
import com.cl.master.domain.User;
import com.cl.master.service.impl.AdminServiceImpl;
import com.cl.master.service.impl.NoteServiceImpl;
import com.cl.master.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.exceptions.TemplateInputException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

/**
 * @Author xc
 * @Date 2023/3/23 17:18
 * @Version 1.0
 */
@Controller
@Slf4j
@EnableAutoConfiguration
public class UserLoginController {
    @Autowired
    private AdminServiceImpl adminService;


    @GetMapping("/login")
    public String login(HttpServletRequest req, HttpServletResponse resp, Model model, HttpSession session) {


        //前端获取数据：
        String username = req.getParameter("username");
        String userpassword = req.getParameter("password");


        //创建实例，接收前端数据：
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(userpassword);

        //调用Service层方法：
        boolean login = adminService.login(admin);

        //判断Service层返回的结果，进行页面的跳转：
        if (login == true) {
            session.setAttribute("loginAdmin", username);
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg", "用户名或密码输入错误");
            return "my/login";
        }

    }



    /*退出登录拦截器*/
    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        /**
         * 由于执行session.invalidate();进行session销毁时报错
         * 所以不执行销毁操作，转而进行清除session所有信息，同样可以达到相同的效果
         */
        Enumeration<String> enumeration = request.getSession().getAttributeNames();
        while (enumeration.hasMoreElements()){
            String key = enumeration.nextElement();
            request.getSession().removeAttribute(key);
        }
        /*重定向至登录页面*/
        /*靠拢真实开发*/
        return "redirect:/login.html";
    }

    /*    跳转至注册页面     */
    @GetMapping("/reg")
    public String reg(){
        return "my/reg";
    }

    /*注册信息*/
    @GetMapping("/end")
    public String end(HttpServletRequest req, HttpServletResponse resp,  Model model) throws IOException {


        //获取前端数据；
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String password2 = req.getParameter("password2");

        if(adminService.getAdminByUsername(username) != null){
            model.addAttribute("msg", "账号已存在，无法再次注册");
            return "my/reg"; //注册失败！
        }
        if(username == null||username == ""||password == null||password == ""){
            model.addAttribute("msg", "账号密码不能为空");
            return "my/reg";
        }

        if (!password.equals(password2)){
            model.addAttribute("msg", "两次密码不一致，请重新输入！");
            return "my/reg";
        }

        //创建实例接收数据：
        Admin admin = new Admin();

        admin.setPassword(password);
        admin.setUsername(username);
        System.out.println("插入数据！！！");
        //判断Service层返回的结果，进行页面的跳转：
        boolean flog = adminService.reg(admin);
        if (flog){
            try
            {
                model.addAttribute("msg", "注册成功");
                return "my/reg";
                // return "redirect:/login.html";
            } catch (Exception e) {
                model.addAttribute("msg", "注册异常！请重新注册");
                return "my/reg";
            }


        } else {
            model.addAttribute("msg", "注册失败！请重新注册");
            return "my/reg"; //注册失败！
        }

    }


    /*注册信息*/
    @GetMapping("/ends")
    public void end(HttpServletRequest req, HttpServletResponse resp,HttpSession session, Model model) throws IOException {

        //获取前端数据；
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //创建实例接收数据：
        Admin admin = new Admin();

        if(adminService.getAdminByUsername(username) != null){
            model.addAttribute("msg", "账号已存在，无法再次注册");
            // return "my/reg"; //注册失败！
            // resp.sendRedirect("my/register");
        }else {
            admin.setPassword(password);
            admin.setUsername(username);
            System.out.println("插入数据！！！");
            //判断Service层返回的结果，进行页面的跳转：
            boolean flog = adminService.reg(admin);
            if (flog){
                try
                {
                    model.addAttribute("msg", "注册成功");
                    resp.sendRedirect("login.html");//注册成功！
                } catch (Exception e) {
                    model.addAttribute("msg", "注册失败！");
                }
            } else {
                resp.sendRedirect("my/register");
            }
        }
    }

    @GetMapping("/form")
    public String form(Model model) {
        model.addAttribute("msg","a");
        return "student/form_student";
    }
}
